package cn.xiaoming.car.server.controller;


import cn.xiaoming.car.pojo.order.pojo.param.OrderTblParam;
import cn.xiaoming.car.server.IOrderService;
import cn.xiaoming.car.server.jsonresult.JsonResult;

import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@Slf4j
@RestController
@RequestMapping("/order/")
@Validated
@Api(tags = "01. 订单模块")
public class OrderController {

    @Autowired
    IOrderService orderService;

    public OrderController() {
        log.debug("创建控制器类对象：OrderController");
    }

    // http://localhost:11000/order/add-new
    @PostMapping("add-new/{carId}")
    @ApiOperation("添加订单")
    @ApiOperationSupport(order = 100)
    public JsonResult addNewOrder(@PathVariable Long carId/*, Long buyerId*/) {//这里在认证信息里获取买家id
        Long buyerId=1L;
        //todo 从认证信息中获取当前用户id，前端传来carId
        log.debug("开始处理【添加订单】的请求，参数：carID:{},buyerID:{}",carId,buyerId);
        orderService.addNewOrder(carId,buyerId);
        return JsonResult.ok("添加订单成功");
    }

    // http://localhost:11000/order/delete
    @PostMapping("delete")
    @ApiOperation("删除订单")
    @ApiOperationSupport(order = 100)
    public JsonResult deleteOrder(String id) {
        log.debug("开始处理【删除订单】的请求，参数：id:{}",id);
        orderService.deleteById(id);
        return JsonResult.ok("删除订单成功");
    }


    // http://localhost:11000/order/{}/update
    @PostMapping("{orderId}/update/{status}")
    @ApiOperation("更新订单状态")
    @ApiOperationSupport(order = 100)
    public JsonResult update(Long userId,@PathVariable String orderId,@PathVariable String status) {
        log.debug("开始处理【修改信息】的请求要修改的订单信息：{},更新状态{}",orderId,status);
        log.debug("开始处理【修改信息】的用户id：{}",userId);
        orderService.updateInfoById(1L, orderId,status);
        return JsonResult.ok();
    }


    //此接口主要提供给管理员
    // http://localhost:11000/order/select-list
    @GetMapping("select-list/{num}")
    @ApiOperation("查询订单列表")
    @ApiOperationSupport(order = 100)
    public JsonResult selectList(@PathVariable Integer num) {
        log.debug("开始处理【查询订单】的请求，参数：请求页码:{}",num);
        return JsonResult.ok(orderService.selectList(num));
    }


    // http://localhost:11000/order/select-list-by-id
    @GetMapping("select-list-by-seller-id/{num}")
    @ApiOperation("根据卖家id查询订单列表")
    @ApiOperationSupport(order = 100)
    public JsonResult selectBySellerId(Long id,@PathVariable Integer num) {
        log.debug("开始处理【根据卖家id查询订单】的请求，参数：请求用户id:{}",id);
        log.debug("参数：请求页码:{}",num);

        //todo 从认证信息中获取到卖家id

        return JsonResult.ok(orderService.selectListByUserId(2L,num,"seller"));
    }

    // http://localhost:11000/order/select-list-by-id
    @GetMapping("select-list-by-buyer-id/{num}")
    @ApiOperation("根据买家id查询订单列表")
    @ApiOperationSupport(order = 100)
    public JsonResult selectByBuyerId(Long id,@PathVariable Integer num) {
        log.debug("开始处理【根据买家id查询订单】的请求，参数：请求用户id:{}",id);
        log.debug("参数：请求页码:{}",num);

        //todo 从认证信息中获取到买家id

        return JsonResult.ok(orderService.selectListByUserId(1L,num,"buyer"));
    }


}
